package com.ruoyi.enterwarehouse.controller;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.GoodsConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.order.OrderUtils;
import com.ruoyi.enterwarehouse.domain.TGoodsBigitImport;
import com.ruoyi.enterwarehouse.domain.WEnterWarehouseImport;
import com.ruoyi.enterwarehouse.domain.TGoodsEnterImport;
import com.ruoyi.goods.domain.*;
import com.ruoyi.goods.service.ITGoodsInfoService;
import com.ruoyi.system.domain.TVehicleDriverInfo;
import com.ruoyi.system.service.ITVehicleDriverInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.enterwarehouse.domain.WEnterWarehouseInfo;
import com.ruoyi.enterwarehouse.service.IWEnterWarehouseInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

/**
 * 入库信息Controller
 * 
 * @author wzy
 * @date 2024-09-02 10:44:55
 */
@RestController
@RequestMapping("/enterwarehouse/enter")
public class WEnterWarehouseInfoController extends BaseController
{
    @Autowired
    private IWEnterWarehouseInfoService wEnterWarehouseInfoService;

    @Autowired
    private ITGoodsInfoService tGoodsInfoService;

    @Autowired
    private ITVehicleDriverInfoService tVehicleDriverInfoService;

    private static Logger logger = LoggerFactory.getLogger(WEnterWarehouseInfoController.class);
    /**
     * 查询入库信息列表
     */
    @PreAuthorize("@ss.hasAnyPermi('enterwarehouse:enter:list, enterwarehouse:plan:list, enterwarehouse:task:list, enterwarehouse:receipt:list')")
    @GetMapping("/list")
    public TableDataInfo list(WEnterWarehouseInfo wEnterWarehouseInfo)
    {
        startPage();
        List<WEnterWarehouseInfo> list = wEnterWarehouseInfoService.selectWEnterWarehouseInfoList(wEnterWarehouseInfo);
        return getDataTable(list);
    }

    /**
     * 导出入库信息列表
     */
    @PreAuthorize("@ss.hasAnyPermi('enterwarehouse:enter:export, enterwarehouse:plan:export, enterwarehouse:task:export, enterwarehouse:receipt:export')")
    @Log(title = "入库信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WEnterWarehouseInfo wEnterWarehouseInfo)
    {
        List<WEnterWarehouseInfo> list = wEnterWarehouseInfoService.selectWEnterWarehouseInfoList(wEnterWarehouseInfo);
        ExcelUtil<WEnterWarehouseInfo> util = new ExcelUtil<WEnterWarehouseInfo>(WEnterWarehouseInfo.class);
        util.exportExcel(response, list, "入库信息数据");
    }

    /**
     * 获取入库信息详细信息
     */
    @PreAuthorize("@ss.hasAnyPermi('enterwarehouse:enter:query, enterwarehouse:plan:query, enterwarehouse:task:query, enterwarehouse:receipt:query')")
    @GetMapping(value = "/{enterWarehouseId}")
    public AjaxResult getInfo(@PathVariable("enterWarehouseId") Long enterWarehouseId)
    {
        return success(wEnterWarehouseInfoService.selectWEnterWarehouseInfoByEnterWarehouseId(enterWarehouseId));
    }

    /**
     * 新增入库信息
     */
    @PreAuthorize("@ss.hasAnyPermi('enterwarehouse:enter:add, enterwarehouse:plan:add, enterwarehouse:task:add, enterwarehouse:receipt:add')")
    @Log(title = "入库信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WEnterWarehouseInfo wEnterWarehouseInfo)
    {
        wEnterWarehouseInfo.setEnterWarehouseNumber(OrderUtils.getOrderNumber("EN", "-"));
        wEnterWarehouseInfo.setCreateBy(getNickName());
        updateMobileAndNumberRelateInfo(wEnterWarehouseInfo);
        return toAjax(wEnterWarehouseInfoService.insertWEnterWarehouseInfo(wEnterWarehouseInfo));
    }

    /**
     * 修改入库信息
     */
    @PreAuthorize("@ss.hasAnyPermi('enterwarehouse:enter:edit, enterwarehouse:plan:edit, enterwarehouse:task:edit, enterwarehouse:receipt:edit')")
    @Log(title = "入库信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WEnterWarehouseInfo wEnterWarehouseInfo)
    {
        // 先判断当前作业在数据库中的状态，防止2人重复操作
        WEnterWarehouseInfo enterWarehouseInfo = wEnterWarehouseInfoService.selectWEnterWarehouseInfoByEnterWarehouseId(wEnterWarehouseInfo.getEnterWarehouseId());
        if(StringUtils.isNull(enterWarehouseInfo)) {
            return error("当前作业，已删除，作业修改失败！");
        } else if(enterWarehouseInfo.getJobStatus().equals("3")) {
            return error("当前作业，已确认入库，修改失败！");
        }

        wEnterWarehouseInfo.setUpdateBy(getNickName());
        return toAjax(wEnterWarehouseInfoService.updateWEnterWarehouseInfo(wEnterWarehouseInfo));
    }

    /**
     * 删除入库信息
     */
    @Transactional
    @PreAuthorize("@ss.hasAnyPermi('enterwarehouse:enter:remove, enterwarehouse:plan:remove, enterwarehouse:task:remove, enterwarehouse:receipt:remove')")
    @Log(title = "入库信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{enterWarehouseIds}")
    public AjaxResult remove(@PathVariable Long[] enterWarehouseIds)
    {
        //List<Long> enterWarehouseIdList = Arrays.asList(enterWarehouseIds);
        for(Long enterWarehouseId : enterWarehouseIds) {
            WEnterWarehouseInfo enterWarehouseInfo = wEnterWarehouseInfoService.selectWEnterWarehouseInfoByEnterWarehouseId(enterWarehouseId);
            if(StringUtils.isNotNull(enterWarehouseInfo) && enterWarehouseInfo.getJobStatus().equals("3")) {
                return error("当前作业，已确认入库，删除失败！");
            }
        }

        tGoodsInfoService.deleteTGoodsInfoByEnterWarehouseIds(1 /*内联删除*/, enterWarehouseIds);
        return toAjax(wEnterWarehouseInfoService.deleteWEnterWarehouseInfoByEnterWarehouseIds(enterWarehouseIds));
    }

    /**
     * 修改入库单状态，确认入库
     */
    //@PreAuthorize("@ss.hasPermi('enterwarehouse:enter:edit')")
    @Log(title = "确认入库", businessType = BusinessType.CONFIRM)
    @PutMapping("/confirm")
    public AjaxResult confirm(@RequestBody WEnterWarehouseInfo wEnterWarehouseInfo)
    {
        Integer rtn = 0;
        if (StringUtils.isNull(wEnterWarehouseInfo)) {
            return error("入库记录不能为空！");
        }

        // 先判断当前作业在数据库中的状态，防止2人重复操作
        WEnterWarehouseInfo enterWarehouseInfo = wEnterWarehouseInfoService.selectWEnterWarehouseInfoByEnterWarehouseId(wEnterWarehouseInfo.getEnterWarehouseId());
        if(enterWarehouseInfo.getJobStatus().equals("3")) {
            return error("当前作业，已确认入库，请勿重复操作！");
        }

        rtn = tGoodsInfoService.selectTGoodsInfoCountByEnterWarehouseNumber(/*2,*/ wEnterWarehouseInfo.getEnterWarehouseNumber());
        if(rtn < 1) {
            return error("货物不能为空！");
        }

        wEnterWarehouseInfo.setEnterWarehouseCustomer(enterWarehouseInfo.getEnterWarehouseCustomer());
        wEnterWarehouseInfo.setEnterWarehouseVehicle(enterWarehouseInfo.getEnterWarehouseVehicle());
        wEnterWarehouseInfo.setEnterWarehouseDriver(enterWarehouseInfo.getEnterWarehouseDriver());
        wEnterWarehouseInfo.setGoodsType(enterWarehouseInfo.getGoodsType());
        wEnterWarehouseInfo.setEnterWarehouseTime(enterWarehouseInfo.getEnterWarehouseTime());
        wEnterWarehouseInfo.setUpdateBy(getNickName());
        wEnterWarehouseInfo.setEnterWarehouseConfirmPerson(getNickName());
        wEnterWarehouseInfo.setEnterWarehouseConfirmTime(DateUtils.getNowDate());

        // 更新货物相关信息
        rtn = tGoodsInfoService.updateTGoodsConfirmRelFieldsByEnterWarehouseNumber(wEnterWarehouseInfo, "已在库");
        if(rtn < 1) {
            logger.error("确认入库，货物信息更新失败。入库单号：" + wEnterWarehouseInfo.getEnterWarehouseNumber());
        }

        // 重复计算，保证数据准确
        rtn = wEnterWarehouseInfoService.updateWEnterTotalWeightPieceByNumber(wEnterWarehouseInfo.getEnterWarehouseNumber());
        if(rtn < 1) {
            logger.error("确认入库，计算入库重量失败。入库单号：" + wEnterWarehouseInfo.getEnterWarehouseNumber());
        }

        // 更新入库单
        wEnterWarehouseInfo.setJobStatus("3");
        rtn = wEnterWarehouseInfoService.updateWEnterWarehouseInfo(wEnterWarehouseInfo);
        if(rtn < 1) {
            logger.error("确认入库，入库作业更新失败。入库单号：" + wEnterWarehouseInfo.getEnterWarehouseNumber());
        }

        return toAjax(rtn);
    }

    /**
     * 修改入库单状态，撤销入库
     */
    //@PreAuthorize("@ss.hasPermi('enterwarehouse:enter:edit')")
    @Log(title = "撤销入库", businessType = BusinessType.REVOKE)
    @PutMapping("/revoke")
    public AjaxResult revoke(@RequestBody WEnterWarehouseInfo wEnterWarehouseInfo)
    {
        Integer rtn = 0;
        /* 检查是否可以撤销，出库中，已出库，货转中，货转过，不允许撤销。 */
        if (StringUtils.isNull(wEnterWarehouseInfo)) {
            return error("入库记录不能为空！");
        }

        // 先判断当前作业在数据库中的状态，防止2人重复操作
        WEnterWarehouseInfo enterWarehouseInfo = wEnterWarehouseInfoService.selectWEnterWarehouseInfoByEnterWarehouseId(wEnterWarehouseInfo.getEnterWarehouseId());
        if(enterWarehouseInfo.getJobStatus().equals("2")) {
            return error("当前单据，已撤销入库，请勿重复操作！");
        }

        //撤销出库判断确认出库时间，出库时间超过2天，不允许撤销
        LocalDate today = LocalDate.now();
        //logger.debug("当前日期: " + today);
        LocalDate enterWarehouseConfirmTime = enterWarehouseInfo.getEnterWarehouseConfirmTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        //logger.debug("确认时间: " + enterWarehouseConfirmTime);
        long daysBetween = ChronoUnit.DAYS.between(enterWarehouseConfirmTime, today);
        if(daysBetween > 4) {
            //logger.debug("时间相差: " + daysBetween);
            //return error("确认入库时间，据当前时间，已经超过4天，不允许撤销");
        }

        int failureNum = 0;
        StringBuilder failureMsg = new StringBuilder();
        List<TGoodsInfo> goodsList = tGoodsInfoService.selectTGoodsInfoByNumber(2, wEnterWarehouseInfo.getEnterWarehouseNumber());
        for (TGoodsInfo goods : goodsList)
        {
            try
            {
                // 检查货物状态，出库中，已出库，货转中，
                if (goods.getInventoryStatus().equals("已在库"))
                {
                    // 检查货物状态，是否货转过
                    if(goods.getGoodsTransferTimes() > 0)
                    {
                        failureNum++;
                        failureMsg.append("<br/>" + failureNum + "、货物 " + goods.getBundleNumber() + " 货转过");
                    }
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、货物 " + goods.getBundleNumber() + " " + goods.getInventoryStatus());
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、入库单据 " + wEnterWarehouseInfo.getEnterWarehouseNumber() + " 撤销失败：";
                failureMsg.append(msg + e.getMessage());
                logger.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，撤销失败！共 " + failureNum + " 条数货物状态不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
//        return successMsg.toString();
//        logger.debug("WWW " + "wEnterWarehouseInfo = " + wEnterWarehouseInfo);
        wEnterWarehouseInfo.setUpdateBy(getNickName());
        wEnterWarehouseInfo.setEnterWarehouseConfirmPerson(null);
        wEnterWarehouseInfo.setEnterWarehouseConfirmTime(null);

        // 更新货物相关信息
        rtn = tGoodsInfoService.updateTGoodsConfirmRelFieldsByEnterWarehouseNumber(wEnterWarehouseInfo, "入库中");
        if(rtn < 1) {
            logger.error("撤销入库，货物信息更新失败。入库单号：" + wEnterWarehouseInfo.getEnterWarehouseNumber());
        }

        // 更新入库单
        wEnterWarehouseInfo.setJobStatus("2");
        rtn = wEnterWarehouseInfoService.updateWEnterWarehouseInfo(wEnterWarehouseInfo);
        if(rtn < 1) {
            logger.error("撤销入库，入库单据更新失败。入库单号：" + wEnterWarehouseInfo.getEnterWarehouseNumber());
        }

        return toAjax(rtn);
    }

    private void updateMobileAndNumberRelateInfo(WEnterWarehouseInfo wEnterWarehouseInfo) {
        TVehicleDriverInfo tVehicleDriverInfo = new TVehicleDriverInfo();

        //更新车牌，司机，电话
        if(!(wEnterWarehouseInfo.getEnterWarehouseVehicle() == null || wEnterWarehouseInfo.getEnterWarehouseVehicle().isEmpty())) {
//            logger.debug("HHH " + "edit = " + tVehicleDriverInfo);
            tVehicleDriverInfo.setVehicleLicenseNumber(wEnterWarehouseInfo.getEnterWarehouseVehicle());
            List<TVehicleDriverInfo> list = tVehicleDriverInfoService.selectTVehicleDriverInfoByNumber(tVehicleDriverInfo);
            tVehicleDriverInfo.setDriverName(wEnterWarehouseInfo.getEnterWarehouseDriver());
            tVehicleDriverInfo.setContactMobile(wEnterWarehouseInfo.getEnterWarehouseMobile());
            if (list.isEmpty()) {
                tVehicleDriverInfo.setCreateBy(getNickName());
                tVehicleDriverInfo.setUpdateBy(null);
                tVehicleDriverInfo.setUpdateBy(null);
                tVehicleDriverInfoService.insertTVehicleDriverInfo(tVehicleDriverInfo);
            } else {
                tVehicleDriverInfo.setVehicleDriverId(list.get(0).getVehicleDriverId());
                tVehicleDriverInfo.setUpdateBy(getNickName());
                tVehicleDriverInfoService.updateTVehicleDriverInfo(tVehicleDriverInfo);
            }
        } else {
            logger.debug("Vehicle HHH Number is empty ");
        }
//
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<TGoodsEnterImport> util = new ExcelUtil<TGoodsEnterImport>(TGoodsEnterImport.class);
        util.importTemplateExcel(response, "货物数据");
    }

    @PostMapping("/importBigitTemplate")
    public void importBigitTemplate(HttpServletResponse response)
    {
        ExcelUtil<TGoodsBigitImport> util = new ExcelUtil<TGoodsBigitImport>(TGoodsBigitImport.class);
        util.importTemplateExcel(response, "货物数据");
    }

    @Log(title = "入库作业", businessType = BusinessType.IMPORT)
//    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, String enterWarehouseNumber) throws Exception
    {
        //避免导入货物，入库单号，为null
        if(enterWarehouseNumber == null || enterWarehouseNumber.isEmpty() || enterWarehouseNumber.equals("null")) {
            return error("入库单号为空，导入失败！");
        }
        //logger.debug("CCC " + "getAvatarPath = " + RuoYiConfig.getImportPath());
        //logger.debug("CCC " + "file = " + file);
        //logger.debug("CCC " + "enterWarehouseNumber = " + enterWarehouseNumber);
        //logger.debug("AAA " + "MimeTypeUtils.IMAGE_EXTENSION = " + MimeTypeUtils.IMAGE_EXTENSION);
        String xmlfile = FileUploadUtils.upload(RuoYiConfig.getImportPath(), file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
        ExcelUtil<TGoodsEnterImport> util = new ExcelUtil<TGoodsEnterImport>(TGoodsEnterImport.class);
        List<TGoodsEnterImport> goodsList = util.importExcel(file.getInputStream());
        //logger.debug("CCC " + "goodsList = " + goodsList);
        String operName = getNickName();
        String message = tGoodsInfoService.importGoods(goodsList, enterWarehouseNumber, operName);
        int rtn = wEnterWarehouseInfoService.updateWEnterTotalWeightPieceByNumber(enterWarehouseNumber);
        if(rtn == 0)
        {
            logger.error("XXX importData " + "update Enter Total Weight Piece error");
        }

        return success(message);
    }

    @Log(title = "入库作业", businessType = BusinessType.IMPORT)
//    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importBigitData")
    public AjaxResult importBigitData(MultipartFile file, String enterWarehouseNumber, String goodsType, Date enterWarehouseTime, String goodsRightCustomer) throws Exception
    {
        //logger.debug("CCC " + "goodsType = " + goodsType);
        //避免导入货物，入库单号，为null
        if(enterWarehouseNumber == null || enterWarehouseNumber.isEmpty() || enterWarehouseNumber.equals("null")) {
            return error("入库单号为空，导入失败！");
        }
        // 先判断当前作业在数据库中的状态，防止2人重复操作
        WEnterWarehouseInfo enterWarehouseInfo = wEnterWarehouseInfoService.selectWEnterWarehouseInfoByEnterWarehouseNumber(enterWarehouseNumber);
        if(StringUtils.isNull(enterWarehouseInfo)) {
            return error("当前作业，已删除，货物导入失败！");
        } else if(enterWarehouseInfo.getJobStatus().equals("3")) {
            return error("当前作业，已确认入库，导入失败！");
        }
        String xmlfile = FileUploadUtils.upload(RuoYiConfig.getImportPath(), file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
        ExcelUtil<TGoodsBigitImport> util = new ExcelUtil<TGoodsBigitImport>(TGoodsBigitImport.class);
        List<TGoodsBigitImport> goodsList = util.importExcel(file.getInputStream(), 1);
        //logger.debug("CCC " + "goodsList = " + goodsList);
        String operName = getNickName();
        String message = tGoodsInfoService.importGoodsBigit(goodsList, enterWarehouseNumber, goodsType, enterWarehouseTime, goodsRightCustomer, operName);
        int rtn = wEnterWarehouseInfoService.updateWEnterTotalWeightPieceByNumber(enterWarehouseNumber);
        if(rtn == 0)
        {
            logger.error("XXX importData " + "update Enter Total Weight Piece error");
        }

        return success(message);
    }

    @PostMapping("/enterTaskTemplate")
    public void enterTemplate(HttpServletResponse response)
    {
        ExcelUtil<WEnterWarehouseImport> util = new ExcelUtil<WEnterWarehouseImport>(WEnterWarehouseImport.class);
        util.importTemplateExcel(response, "入库导入数据");
    }

    @Log(title = "入库作业", businessType = BusinessType.IMPORT)
//    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importEnterTask")
    public AjaxResult importEnter(MultipartFile file) throws Exception
    {
//        logger.debug("CCC " + "getAvatarPath = " + RuoYiConfig.getImportPath());
//        logger.debug("CCC " + "file = " + file);
        //logger.debug("AAA " + "MimeTypeUtils.IMAGE_EXTENSION = " + MimeTypeUtils.IMAGE_EXTENSION);
        String xmlfile = FileUploadUtils.upload(RuoYiConfig.getImportPath(), file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);

        ExcelUtil<WEnterWarehouseImport> util = new ExcelUtil<WEnterWarehouseImport>(WEnterWarehouseImport.class);
        List<WEnterWarehouseImport> enterList = util.importExcel(file.getInputStream());
//        logger.debug("CCC " + "enterList = " + enterList);

        String operName = getNickName();
        String message = wEnterWarehouseInfoService.importEnterTask(enterList, operName);
        return success(message);
    }

    /**
     * 获取货物重量合计
     */
    @GetMapping(value = "/enterweightsum")
    public AjaxResult enterWeightSum(WEnterWarehouseInfo wEnterWarehouseInfo)
    {
        List<TGoodsWeightSum> list = wEnterWarehouseInfoService.getWeightSumByEnterWarehouseInfo(wEnterWarehouseInfo);
//        logger.debug("GGG " + "getWeightSumByEnterWarehouseInfo = " + list);
        return success(list);
    }

    /**
     * 交换重量
     */
    @GetMapping(value = "/exchange/{enterWarehouseId}")
    public AjaxResult exchange(@PathVariable("enterWarehouseId") Long enterWarehouseId)
    {
        double weight = 0.0;
        WEnterWarehouseInfo wEnterWarehouseInfo = wEnterWarehouseInfoService.selectWEnterWarehouseInfoByEnterWarehouseId(enterWarehouseId);
        List<TGoodsInfo> goods = tGoodsInfoService.selectTGoodsInfoByNumber(2, wEnterWarehouseInfo.getEnterWarehouseNumber());
        for (TGoodsInfo good : goods) {
            weight = good.getWeight();
            good.setWeight(good.getTheoryWeight());
            good.setTheoryWeight(weight);
            tGoodsInfoService.updateTGoodsInfo(good);
        }
        weight = wEnterWarehouseInfo.getWeight();
        wEnterWarehouseInfo.setWeight(wEnterWarehouseInfo.getTheoryWeight());
        wEnterWarehouseInfo.setTheoryWeight(weight);
        return success(wEnterWarehouseInfoService.updateWEnterWarehouseInfo(wEnterWarehouseInfo));
    }

    // 以下将入库作业，货物增删改，导入接口移到此处。
    // 货物，增删改导，均需判断，入库作业状态，如果已确认入库，则返回，操作失败。

    /**
     * 入库作业，新增货物信息
     */
//    @PreAuthorize("@ss.hasPermi('goods:good:add')")
    @Log(title = "货物信息", businessType = BusinessType.INSERT)
    @PostMapping("/ext")
    public AjaxResult add(@RequestBody TGoodsInfo tGoodsInfo)
    {
        // 判断输入参数
        if(StringUtils.isNull(tGoodsInfo) || StringUtils.isNull(tGoodsInfo.getEnterWarehouseNumber())) {
            return error("入库参数为空，货物新增失败！");
        }
        // 先判断当前作业在数据库中的状态，防止2人重复操作
        WEnterWarehouseInfo enterWarehouseInfo = wEnterWarehouseInfoService.selectWEnterWarehouseInfoByEnterWarehouseNumber(tGoodsInfo.getEnterWarehouseNumber());
        if(StringUtils.isNull(enterWarehouseInfo)) {
            return error("当前作业，已删除，货物新增失败！");
        } else if(enterWarehouseInfo.getJobStatus().equals("3")) {
            return error("当前作业，已确认入库，新增失败！");
        }

        if(GoodsConstants.NOT_UNIQUE.equals(tGoodsInfoService.checkBundleNumberUnique(tGoodsInfo)))
        {
            //return error("新增货物'" + tGoodsInfo.getBundleNumber() + "'失败，捆包号已存在");
            return error("捆包号已存在" + tGoodsInfo.getBundleNumber());
        }
        tGoodsInfo.setCreateBy(getNickName());
        int rtn = tGoodsInfoService.insertTGoodsInfo(tGoodsInfo);
        if((rtn > 0) && StringUtils.isNotNull(tGoodsInfo.getEnterWarehouseNumber()))
        {
            rtn = wEnterWarehouseInfoService.updateWEnterTotalWeightPieceByNumber(tGoodsInfo.getEnterWarehouseNumber());
            if(rtn == 0)
            {
                logger.error("enter warehouse Add goods " + "update Enter Total Weight Piece error, EnterWarehouseNumber " + tGoodsInfo.getEnterWarehouseNumber());
            }
        } else {
            logger.error("enter warehouse Add goods " + "insertTGoodsInfo error, tGoodsInfo " + tGoodsInfo);
        }

        return toAjax(rtn);
    }

    /**
     * 入库作业，修改货物信息
     */
//    @PreAuthorize("@ss.hasPermi('goods:good:edit')")
    @Log(title = "货物信息", businessType = BusinessType.UPDATE)
    @PutMapping("/ext")
    public AjaxResult edit(@RequestBody TGoodsInfo tGoodsInfo)
    {
        //logger.error("XXX edit tGoodsInfo " + tGoodsInfo);
        // 判断输入参数
        if(StringUtils.isNull(tGoodsInfo) || StringUtils.isNull(tGoodsInfo.getEnterWarehouseNumber())) {
            return error("入库参数为空，货物修改失败！");
        }
        // 先判断当前作业在数据库中的状态，防止2人重复操作
        WEnterWarehouseInfo enterWarehouseInfo = wEnterWarehouseInfoService.selectWEnterWarehouseInfoByEnterWarehouseNumber(tGoodsInfo.getEnterWarehouseNumber());
        if(StringUtils.isNull(enterWarehouseInfo)) {
            return error("当前作业，已删除，货物修改失败！");
        } else if(enterWarehouseInfo.getJobStatus().equals("3")) {
            return error("当前作业，已确认入库，修改失败！");
        }

        if (GoodsConstants.NOT_UNIQUE.equals(tGoodsInfoService.checkBundleNumberUnique(tGoodsInfo)))
        {
            //return error("修改货物'" + tGoodsInfo.getBundleNumber() + "'失败，捆包号已存在");
            return error("捆包号已存在" + tGoodsInfo.getBundleNumber());
        }
        tGoodsInfo.setUpdateBy(getNickName());
        int rtn = tGoodsInfoService.updateTGoodsInfo(tGoodsInfo);
        if((rtn > 0) && StringUtils.isNotNull(tGoodsInfo.getEnterWarehouseNumber()))
        {
            if(wEnterWarehouseInfoService.updateWEnterTotalWeightPieceByNumber(tGoodsInfo.getEnterWarehouseNumber()) == 0)
            {
                logger.error("enter warehouse Edit goods " + "update Enter Total Weight Piece error, EnterWarehouseNumber " + tGoodsInfo.getEnterWarehouseNumber());
            }
        } else {
            logger.error("enter warehouse Edit goods " + "updateTGoodsInfo error, tGoodsInfo " + tGoodsInfo);
        }

        return toAjax(rtn);
    }

    /**
     * 入库作业，删除货物信息，物理删除
     */
    //@PreAuthorize("@ss.hasPermi('goods:good:remove')")
    @Log(title = "货物信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/ext/{goodsIds}/{enterWarehouseNumber}")
    public AjaxResult removeExt(@PathVariable Long[] goodsIds, @PathVariable String enterWarehouseNumber)
    {
        //logger.info("enter warehouse removeExt " + "goodsIds = " + Arrays.toString(goodsIds) + " enterWarehouseNumber = " + enterWarehouseNumber);
        // 判断输入参数
        if(StringUtils.isNull(goodsIds) || StringUtils.isNull(enterWarehouseNumber)) {
            return error("入库参数为空，货物删除失败！");
        }
        // 先判断当前作业在数据库中的状态，防止2人重复操作
        WEnterWarehouseInfo enterWarehouseInfo = wEnterWarehouseInfoService.selectWEnterWarehouseInfoByEnterWarehouseNumber(enterWarehouseNumber);
        if(StringUtils.isNull(enterWarehouseInfo)) {
            return error("当前作业，已删除，货物删除失败！");
        } else if(enterWarehouseInfo.getJobStatus().equals("3")) {
            return error("当前作业，已确认入库，删除失败！");
        }
/*        TGoodsInfo tGoodsInfo = tGoodsInfoService.selectTGoodsInfoByGoodsId(goodsIds[0]);

        int rtn = tGoodsInfoService.deleteTGoodsInfoExtByGoodsIds(goodsIds);
        if((rtn > 0) && StringUtils.isNotNull(tGoodsInfo) && StringUtils.isNotNull(tGoodsInfo.getEnterWarehouseNumber()))
        {
            rtn = wEnterWarehouseInfoService.updateWEnterTotalWeightPieceByNumber(tGoodsInfo.getEnterWarehouseNumber());
            if(rtn == 0)
            {
                logger.error("enter warehouse Delete goods " + "update Enter Total Weight Piece error");
            }
        } else {
            logger.error("enter warehouse Delete goods " + "deleteTGoodsInfoExtByGoodsIds error, enterWarehouseNumber " + tGoodsInfo.getEnterWarehouseNumber() + ", goodsIds, " + Arrays.toString(goodsIds));
        }
*/
        int rtn = tGoodsInfoService.deleteTGoodsInfoExtByGoodsIds(goodsIds, enterWarehouseNumber);
        if((rtn > 0) && StringUtils.isNotNull(enterWarehouseNumber))
        {
            rtn = wEnterWarehouseInfoService.updateWEnterTotalWeightPieceByNumber(enterWarehouseNumber);
            if(rtn == 0)
            {
                logger.error("enter warehouse Delete goods " + "update Enter Total Weight Piece error");
            }
        } else {
            logger.error("enter warehouse Delete goods " + "deleteTGoodsInfoExtByGoodsIds error, enterWarehouseNumber " + enterWarehouseNumber + ", goodsIds, " + Arrays.toString(goodsIds));
        }

        return toAjax(rtn);
    }

}
